//
//  QCounterView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/16.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QCounterView.h"

@implementation QCounterView{

    NSArray *_titleArr;
}

- (instancetype)initWithFrame:(CGRect)frame andBlock:(ChooseBlock)block{

    self = [super initWithFrame:frame];
    if(self){
        self.layer.masksToBounds = YES;
        _cblock = block;
        _titleArr = @[@"+",@"-",@"×",@"÷",@"1",@"2",@"3",@"",@"4",@"5",@"6",@"AC",@"7",@"8",@"9",@"=",@".",@"0",@"",@""];
        [self createUI];
    }
    return self;
}

- (void)createUI{

    int w = kScreenWidth/4;
    int h = self.frame.size.height/5;

    for(int i=0; i<20; ++i){
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i%4*w, i/4*h, w, h)];
        [button setBackgroundImage:[QColorUtility buttonImageFromColor:[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1] rect:CGRectMake(0, 0, kScreenWidth/4,self.frame.size.height/5)] forState:UIControlStateNormal];
        
        [button setBackgroundImage:[QColorUtility buttonImageFromColor:[UIColor orangeColor] rect:CGRectMake(0, 0, kScreenWidth/4,self.frame.size.height/5)] forState:UIControlStateHighlighted];
        
        [button setTitleColor:[QColorUtility defaultBackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        button.layer.borderWidth = 0.3;
        button.layer.borderColor = [QColorUtility defaultLowGrayColor].CGColor;
        button.tag = i;
        [button setTitle:_titleArr[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:28.0];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        if(button.tag == 18 || button.tag == 19){
            button.hidden = YES;
        }else{
            button.hidden = NO;
        }
        if(button.tag == 7){
            [button setImage:[UIImage imageNamed:@"delete_image"] forState:UIControlStateNormal];
        }
    }
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(w*2, h*4, w*2, h)];
    [button setBackgroundImage:[QColorUtility buttonImageFromColor:[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1] rect:CGRectMake(0, 0, kScreenWidth/4,self.frame.size.height/5)] forState:UIControlStateNormal];
    [button setBackgroundImage:[QColorUtility buttonImageFromColor:[UIColor orangeColor] rect:CGRectMake(0, 0, kScreenWidth/4,self.frame.size.height/5)] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    button.layer.borderWidth = 0.3;
    button.layer.borderColor = [QColorUtility defaultLowGrayColor].CGColor;
    button.tag = 20;
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:23.0];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];

}

- (void)buttonAction:(UIButton *)button{
    if(_cblock){
        _cblock(button.tag);
    }
}

@end
